﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework;

namespace TByte.XnaHelper
{
    public static class Assets
    {
        static Dictionary<String, SpriteSheet> SpriteSheets = new Dictionary<string, SpriteSheet>();
        static Dictionary<String, Texture2D> Textures = new Dictionary<string, Texture2D>();
        static Dictionary<String, SpriteFont> Fonts = new Dictionary<string, SpriteFont>();

        public static void Load(string assetName)
        {
            if (!File.Exists(Manager.Content.RootDirectory + "\\" + assetName + ".xnb"))
                throw new IOException("This file does not exist.");
            string[] parts = assetName.Split('_');
            if (parts.Count() != 2)
                throw new ArgumentException("Invalid asset name.");

            switch (parts[0])
            {
                default:
                    TByte.XnaHelper.GameConsole.WriteLine("The asset type '" + parts[0] + "' does not exist.");
                    break;
                case "sprite":
                    Textures[parts[1]] = Manager.Content.Load<Texture2D>(assetName);
                    break;
                case "font":
                    Fonts[parts[1]] = Manager.Content.Load<SpriteFont>(assetName);
                    break;
                case "sheet":
                    string[] sheetParts = parts[1].Split('@');
                    if (sheetParts.Count() != 2)
                    {
                        throw new ArgumentException("No sheet info was given!");
                    }
                    string[] sheetInfo = sheetParts[1].Split('x');

                    int columns;
                    int rows;
                    if (Int32.TryParse(sheetInfo[0], out columns))
                    {
                        if (sheetInfo.Count() == 2 && Int32.TryParse(sheetInfo[1], out rows))
                        {
                            SpriteSheets[sheetParts[0]] = new SpriteSheet(Manager.Content.Load<Texture2D>(assetName), columns, rows);
                        }
                        else
                        {
                            SpriteSheets[sheetParts[0]] = new SpriteSheet(Manager.Content.Load<Texture2D>(assetName), columns, 1);
                        }
                    }
                    else
                    {
                        throw new ArgumentException("You need to supply sheet information when loading a spriteSheet.");
                    }
                    break;

            }
        }

        public static Texture2D Texture(string name)
        {
            if (!Textures.ContainsKey(name))
            {
                Load("sprite_" + name);
            }

            return Textures[name];
        }
        public static SpriteFont Font(string name)
        {
            if (!Fonts.ContainsKey(name))
            {
                Load("font_" + name);
            }

            return Fonts[name];
        }
        public static SpriteSheet SpriteSheet(string name)
        {
            if (!SpriteSheets.ContainsKey(name))
            {
                //Need to load sheet.
                
                //Fist find the file
                string[] files = Directory.GetFiles(Manager.Content.RootDirectory, "sheet_" + name + "@*");

                //Load first occurance if it exists
                if (files.Count() == 0)
                    throw new IOException("No such sheet file was found.");

                string[] parts = files[0].Split('\\');
                string fileName = parts[parts.Count() - 1].Split('.')[0];
                Load(fileName);
            }
            return SpriteSheets[name];
        }
    }
}
